package kin.test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import kin.converter.BeanTypeConverter;
import kin.json.FooJsonSerializer;
import kin.json.JsonUtils;
import kin.json.MyBeanSerializerModifier;
import kin.json.NullSerializer;

import kin.json.MyNameStrategy;



import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;




public class t2 {
public static void main(String[] args) {
	JsonFactory jf= new JsonFactory();
     
	StringWriter out=new StringWriter();
    try {
    	
		JsonGenerator jg=jf.createGenerator(out);
		
		//jg.configure(arg0, arg1)
		
		 //jg.configure(Feature.QUOTE_FIELD_NAMES,true);
		ObjectMapper om=new ObjectMapper();
		//om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
	//om.registerSubtypes(Us.class);
		om.setPropertyNamingStrategy(new MyNameStrategy());
		
		/*om.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
           @Override
			public void serialize(Object value, JsonGenerator jgen,
					SerializerProvider provider) throws IOException,
					JsonProcessingException {
				jgen.writeString(""); 
				
		}});*/
		//BeanSerializerFactory.instance.createTypeSerializer(null, baseType)
		SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());
		
		om.setSerializerFactory(serializerFactory);
		 //om.getSerializerProvider().setNullValueSerializer(new NullSerializer());
		
		
		//SubtypeResolver str = null;
		/*TypeFactory f = TypeFactory.defaultInstance();
		System.out.println(TypeFactory.unknownType());
		om.setTypeFactory(f);*/
		//om.setSubtypeResolver(str);
	om.registerSubtypes(Us.class);
	om.registerSubtypes(Mo.class);
		
	// DefaultSerializerProvider p = null;
	// p.
	//om.setSerializerProvider(p);
	
		Us u=new Us();
		u.setUs_id(1);
		List<Us> child=new ArrayList();
		Us uc1=new Us();
		uc1.setUs_id(3);
		Us uc2=new Us();
		uc2.setUs_id(4);
		child.add(uc1);
		child.add(uc2);
		
		
		u.setChildren(child);
		Us up=new Us();
		
		up.setUs_id(0);
		u.setParent(up);
		Mo mo=new Mo();
		mo.setMo_id(2);
		mo.setName("mo");
		
		//{"us_id":1,"aaa":null,"children":[],"m":null,"list":null,"parent":0,"mo_id":2}
		//{"us_id":1,"aaa":null,"children":[],"m":null,"list":null,"parent":0,"mo_id":2}
		u.setMo(mo);
		//System.out.println("eee:"+om.valueToTree(u));
		
		//om.writeTree(jgen, rootNode)
		//MySubTypeResolver str = new MySubTypeResolver();
		//t2.scanPackage("kin.test", str);
		
		//System.out.println(om.getSubtypeResolver());
		//om.setSubtypeResolver(str);
		
		om.writeValue(jg, u);
	
		//System.out.println(JsonUtils.jsonFromObject(u));
		
		System.out.println(out.toString());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

public static void scanPackage(String packageName,SubtypeResolver str){
	ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
	scanner.addIncludeFilter(new AnnotationTypeFilter(javax.persistence.Entity.class));
	for (BeanDefinition beanDefinition : scanner.findCandidateComponents(packageName)) {
		Class cls=null;
		try {
			cls=Class.forName(beanDefinition.getBeanClassName());
		
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//module.Serializer(new MyJsonSerializer());
		str.registerSubtypes(cls);
		
		//module.addSerializer(cls, new MyJsonSerializer());
	}
}
}
